const express = require('express')
const router = express.Router();
const fs = require('fs')
const userList = require('../mock/user.json')
const list = require('../mock/list.json')


const successModel = {
  code: 0,
  msg: 'success',
}
const failModel = {
  code: -1,
  msg: 'fail',
}

router.post('/login', (req, res, next) => {
  const { phone, pwd } = req.body
  let flag = userList.some(val => val.phone === phone && val.pwd === pwd)
  if (flag) {
    res.send(successModel)
  } else {
    res.send(failModel)
  }
})

router.post('/add', (req, res, next) => {
  // req.body;
  let id = list.length;
  list.push({ id, ...req.body })
  fs.writeFileSync('./mock/list.json', JSON.stringify(list))
  res.send({ id: id, ...successModel })
})

module.exports = router;